package com.gen.tool;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.ouken.com.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;





/**
 * 声音控制类
 * @author wyf
 *
 */
public class SoundPlayer {

    private static MediaPlayer music;
    private static MediaPlayer c_waiting_music;
    private static MediaPlayer p_drawing_music;
    
    private static SoundPool soundPool;
    
    private static boolean musicSt = true; //音乐开关
    private static boolean soundSt = true; //音效开关
    private static Context context;
    
    private static final int[] musicId = {R.raw.pinkpanther_background};
    private static final int[] c_waiting_musicId =	{R.raw.pencildraw_chanllengerwaiting};
    private static final int[] p_drawing_musicId =	{R.raw.timeclock_providerdrawing};
  
    private static Map<Integer,Integer> soundMap; //音效资源id与加载过后的音源id的映射关系表
    
    private static float soundVol;
    private static boolean isMusicStopped = false;
    private static boolean is_p_drawing_music_Stopped = false;
    private static boolean is_c_waiting_music_Stopped = false;
    /**
     * 初始化方法
     * @param c
     */
    public static void init(Context c)
    {
        context = c;

        initMusic();
        
        init_c_waiting_music();
        init_p_drawing_music();
        
        initSound();
    }
    
    //初始化音效播放器
    private static void initSound()
    {
        soundPool = new SoundPool(10,AudioManager.STREAM_MUSIC,100);
        
        soundMap = new HashMap<Integer,Integer>();
        soundMap.put(R.raw.keypress_common, soundPool.load(context, R.raw.keypress_common, 1));
        soundMap.put(R.raw.correct_chanllengerplaying, soundPool.load(context, R.raw.correct_chanllengerplaying, 1));
        soundMap.put(R.raw.myturn_chanllengerplaying, soundPool.load(context, R.raw.myturn_chanllengerplaying, 1));
        soundMap.put(R.raw.startgame_entergameroom, soundPool.load(context, R.raw.startgame_entergameroom, 1));
        soundMap.put(R.raw.incorrect_challengerplaying, soundPool.load(context, R.raw.incorrect_challengerplaying, 1));
        soundMap.put(R.raw.winner, soundPool.load(context, R.raw.winner, 1));
        soundMap.put(R.raw.fail, soundPool.load(context, R.raw.fail, 1));
        /*soundMap.put(R.raw.pencildraw_chanllengerwaiting, soundPool.load(context, R.raw.pencildraw_chanllengerwaiting, 2));
        soundMap.put(R.raw.timeclock_providerdrawing, soundPool.load(context, R.raw.timeclock_providerdrawing, 2));*/
        soundVol = 1;
        
    }
    
    //初始化音乐播放器
    private static void initMusic()
    {
        int r = new Random().nextInt(musicId.length);
        music = MediaPlayer.create(context,musicId[r]);
        music.setLooping(true);
    }
    
    private static void init_c_waiting_music()
    {
        int r = new Random().nextInt(c_waiting_musicId.length);
        c_waiting_music = MediaPlayer.create(context,c_waiting_musicId[r]);
        c_waiting_music.setLooping(true);
    }
    
    private static void init_p_drawing_music()
    {
        int r = new Random().nextInt(p_drawing_musicId.length);
        p_drawing_music = MediaPlayer.create(context,p_drawing_musicId[r]);
        p_drawing_music.setLooping(true);
    }
    
    
    /**
     * 播放音效
     * @param resId 音效资源id
     */
    public static void playSound(int resId)
    {
        if(soundSt == false)
            return;
        
        Integer soundId = soundMap.get(resId);
        if(soundId != null)
            soundPool.play(soundId, soundVol, soundVol, 1, 0, 1);
    }

    /**
     * 暂停音乐
     */
    public static void pauseMusic()
    {
        if(music.isPlaying())
            music.pause();
        System.out.println("music paused");
    }
    
    public static void pause_c_waiting_music()
    {
        if(c_waiting_music.isPlaying())
        	c_waiting_music.pause();
        System.out.println("c_waiting_music paused");
    }
    
    public static void pause_p_drawing_music()
    {
        if(p_drawing_music.isPlaying())
        	p_drawing_music.pause();
        System.out.println("p_drawing_music paused");
    }
    
    
    /**
     * 播放音乐
     */
    public static void startMusic()
    {
        if(musicSt){
        	if(isMusicStopped){
        		initMusic();
        	}
        	music.start();
        	isMusicStopped = false;
        }
            
        System.out.println("started");
    }
    
    public static void start_c_waiting_music()
    {
        if(musicSt){
        	if(is_c_waiting_music_Stopped){
        		init_c_waiting_music();
        	}
        	c_waiting_music.start();
        	is_c_waiting_music_Stopped = false;
        }
            
        System.out.println("started");
    }
    
    public static void start_p_drawing_music()
    {
        if(musicSt){
        	if(is_p_drawing_music_Stopped){
        		init_p_drawing_music();
        	}
        	p_drawing_music.start();
        	is_p_drawing_music_Stopped = false;
        }
            
        System.out.println("p_drawing_music started");
    }
    
    
    public static void stopMusic(){
    	if(music.isPlaying())
            music.stop();
    	isMusicStopped = true;
        System.out.println("stopped");
    }
    
    public static void stop_c_waiting_music(){
    	if(c_waiting_music.isPlaying())
    		c_waiting_music.stop();
    	is_c_waiting_music_Stopped = true;
        System.out.println("c_waiting_music stopped");
    }
    
    public static void stop_p_drawing_music(){
    	if(p_drawing_music.isPlaying())
    		p_drawing_music.stop();
    	is_p_drawing_music_Stopped = true;
        System.out.println("p_drawing_music stopped");
    }
    
    /**
     * 切换一首音乐并播放
     */
    public static void changeAndPlayMusic()
    {
        if(music != null)
            music.release();
        initMusic();
        startMusic();
    }
    
    /**
     * 获得音乐开关状态
     * @return
     */
    public static boolean isMusicSt() {
        return musicSt;
    }
    
    /**
     * 设置音乐开关
     * @param musicSt
     */
    public static void setMusicSt(boolean musicSt) {
        SoundPlayer.musicSt = musicSt;
        if(musicSt)
            startMusic();
        else
            stopMusic();
    }

    /**
     * 获得音效开关状态
     * @return
     */
    public static boolean isSoundSt() {
        return soundSt;
    }

    /**
     * 设置音效开关
     * @param soundSt
     */
    public static void setSoundSt(boolean soundSt) {
        SoundPlayer.soundSt = soundSt;
    }
    
    /**
     * 发出‘邦’的声音
     */
    public static void keypress()
    {
        playSound(R.raw.keypress_common);
    }
    
    public static void corretAnswer(){
    	playSound(R.raw.correct_chanllengerplaying);
    }
    
    public static void myturn(){
    	playSound(R.raw.myturn_chanllengerplaying);
    }
    
    public static void startgame(){
    	playSound(R.raw.startgame_entergameroom);
    }
    
    public static void setMusicVol(float vol){
    	music.setVolume(vol, vol);
    } 
    
    public static void setSoundVol(float vol){
    	soundVol = vol;
    }
}